22. 练习 — Turtle 方法

练习 - Turtle 方法

你已经学习了使用 turtle 执行不同操作的大量不同方法。我们复习下这些方法,确保你已经记住它们。

QUIZ QUESTION: :

将方法调用与作用相匹配。

ANSWER CHOICES:



作用

方法调用

amy.forward(100)

amy.back(100)

amy.left(100)

amy.color("blue")

SOLUTION:

作用

方法调用

amy.forward(100)

amy.back(100)

amy.left(100)

amy.color("blue")

QUIZ QUESTION: :

以下是你刚刚学习过的方法。

同样,将方法调用与作用相匹配。

ANSWER CHOICES:



作用

方法调用

amy.penup()

amy.speed(0)

amy.width(5)

amy.pendown()

SOLUTION:

作用

方法调用

amy.penup()

amy.speed(0)

amy.width(5)

amy.pendown()

现在该你来练习输入方法了。这样有助于你之后需要这些方法时,记住正确的语法。

QUESTION:

输入使 amy 向前移动 100 个像素的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入使 amy 后退 100 个像素的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入使 amy 左转 100 度的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入将 amy 的绘制颜色设为蓝色的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

有时候,我们希望 turtle 在屏幕上到处移动但是不绘制任何线条。请输入关闭 amy 的笔的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入重新开启 amy 的笔的方法调用,以便在她移动时绘制图形。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入使 amy 尽可能快速绘制的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

QUESTION:

输入将 amy 的线条粗细更改为 5 个像素的方法调用。

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

现在我们使用这些方法绘制简单的图形:三条彩色线条,中间有空格,如下所示:

在下面的 workspace 中,看看你能否自己画出该图形。我们添加了注释,提醒你应该采取的步骤

(如果遇到问题,请在 workspace 下方查找我们的解决方案。)

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: html-live
  • Opened files (when workspace is loaded): n/a

备注 :如果你无法打开上面的workspace,请去 这里


## ⚠️ 剧透! **下面是我们的解决方案。**如果你能认真完成练习,然后再将你的代码与我们的代码进行对比,学习效果将更好!

----

解决方案

import turtle
amy = turtle.Turtle()

# Make the width thicker so that the line will be easier to see
# 使线条宽度加粗,以便更容易看到线条
amy.width(5)

# Move back without drawing anything
# 向后移动且不画任何东西
amy.penup()
amy.back(140)
amy.pendown()

# Draw a red line
# 画一条红线(red)
amy.color("red")
amy.forward(50)

# Move forward without drawing anything
# 向前移动且不画任何东西
amy.penup()
amy.forward(50)
amy.pendown()

# Draw an orange line
# 画一条橙色线(orange)
amy.color("orange")
amy.forward(50)

# Move forward without drawing anything
# 向前移动且不画任何东西
amy.penup()
amy.forward(50)
amy.pendown()

# Draw a yellow line
# 画一条黄线(yellow)
amy.color("yellow")
amy.forward(50)

参考资料

我们希望这些练习能帮你记住如何使用所学的各种方法。我们还整理了一个PDF文件,列出了我们到目前为止介绍的所有方法,你可以在 这里 找到。请随意下载或打印,以备将来参考。

您也可以在任何时候通过进入左边的 Resources 选项卡 (在左边菜单的顶部) 下载它。

如果你需要阅读中文的文档,可以在 这里 翻阅相关的内容。